package com.yuke.cloud.service.omc;

import com.alibaba.fastjson.JSONArray;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.omc.entity.OdsOrder;

import java.util.*;

public class test extends BaseService<OdsOrder> {


    public static void main(String[] args) {
        new test().te();
    }

    public void te() {
        String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
                "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]";


        String s = " [{\n" +
                "\t\t\"cartId\": \"875605338115\",\n" +
                "\t\t\"itemId\": \"530425196249\",\n" +
                "\t\t\"productId\": \"3588238927936\",\n" +
                "\t\t\"quantity\": \"3\",\n" +
                "\t\t\"createTime\": 1535971872000,\n" +
                "\t\t\"goodsPrice\": \"2\"\n" +
                "\t}, {\n" +
                "\t\t\"cartId\": \"875602230844\",\n" +
                "\t\t\"itemId\": \"530425196249\",\n" +
                "\t\t\"productId\": \"3649785824979\",\n" +
                "\t\t\"quantity\": \"1\",\n" +
                "\t\t\"createTime\": 1535971888000,\n" +
                "\t\t\"goodsPrice\": \"2\"\n" +
                "\t}, {\n" +
                "\t\t\"cartId\": \"876353087440\",\n" +
                "\t\t\"itemId\": \"530425196249\",\n" +
                "\t\t\"productId\": \"3790098972071\",\n" +
                "\t\t\"quantity\": \"1\",\n" +
                "\t\t\"createTime\": 1536026307000,\n" +
                "\t\t\"goodsPrice\": \"2\"\n" +
                "\t}, {\n" +
                "\t\t\"cartId\": \"876018095012\",\n" +
                "\t\t\"itemId\": \"575366097859\",\n" +
                "\t\t\"productId\": \"3777025517011\",\n" +
                "\t\t\"quantity\": \"1\",\n" +
                "\t\t\"createTime\": 1535971809000,\n" +
                "\t\t\"goodsPrice\": \"5\"\n" +
                "\t}, {\n" +
                "\t\t\"cartId\": \"875637931159\",\n" +
                "\t\t\"itemId\": \"575366097859\",\n" +
                "\t\t\"productId\": \"3924930362809\",\n" +
                "\t\t\"quantity\": \"1\",\n" +
                "\t\t\"createTime\": 1535971824000,\n" +
                "\t\t\"goodsPrice\": \"8\"\n" +
                "\t}]";
        //第一种方式
        Set<String> set = new HashSet<String>();
        Set<String> set2 = new HashSet<String>();
        Map<String, Integer> map = new HashMap<String, Integer>();
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(s);
        System.out.println("利用JSONArray中的parse方法来解析json数组字符串");
        for (Map<String, String> mapList : listObjectFir) {
            for (Map.Entry entry : mapList.entrySet()) {


                set.add(String.valueOf(entry.getValue()));
            }
        }

//价格集合
        List<Integer> price6 = new ArrayList<Integer>();
//数量
        List<Integer> num6 = new ArrayList<Integer>();
        //商店集合
        List<String> shop = new ArrayList<String>();
        List<Long> things = new ArrayList<Long>();
        for (Map<String, String> mapList : listObjectFir) {

            for (Map.Entry entry : mapList.entrySet()) {
                for (String str : set) {

                    if (str.equals(entry.getValue())) {
                        int price = Integer.valueOf(mapList.get("goodsPrice"));
                        int num = Integer.valueOf(mapList.get("quantity"));
                        long spid = Long.valueOf(mapList.get("productId"));
                        int price2 = price * num;
                        //System.out.println(price+"      "+num+"      "+str);
                        price6.add(price);
                        shop.add(str);
                        num6.add(num);
                        things.add(spid);
                    }
                }
                break;

            }

        }


        List<OdsOrder> ji = new ArrayList<OdsOrder>();

        long userid = 888;
        //先生成订单，再生成订单详情，要根据订单id生成订单详情ID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        List shopreal = new ArrayList();
        for (int i = 0; i < shop.size(); i++) {
            if (!shopreal.contains(shop.get(i))) {
                shopreal.add(shop.get(i));
            }
        }

        for (int i = 0; i < shopreal.size(); i++) {
            int jiage = 0;
            OdsOrder order = new OdsOrder();
            order.setOrderSn(shopreal.get(i) + UUID.randomUUID().toString().replaceAll("-", ""));
            order.setUserId(userid);
            order.setOrderType((byte) 0);

            order.setShopId(Long.valueOf(shopreal.get(i).toString()));

            for (int y = 0; y < shop.size(); y++) {

                if (shop.get(y).equals(shopreal.get(i).toString())) {
                    jiage += price6.get(y) * num6.get(y);
                }
            }
            order.setProductPriceTotal(jiage);
            order.setAmountPayable(jiage);
            order.setAmountPaid(0);
            order.setDiscount(0);
            order.setOrderStatus((byte) 0);
            order.setPayStatus((byte) 0);
            order.setShippingType((byte) 0);
            order.setExpressId((long) 0);
            order.setShippingStatus((byte) 0);
            order.setBestShippingTime(new Date());
            order.setConsignee("sun");
            order.setSex((byte) 0);
            order.setMobile("182");
            order.setEmail("144");
            order.setTelephone("156");
            order.setRegionCountryId((long) 12);
            order.setPayStatus((byte) 0);
            order.setPayStatus((byte) 0);
            //收货人的地址，邮政编码调用另一个方法，当发货的时候补全
            ji.add(order);
            if (order != null) {
                System.out.println(order.getOrderSn());
            }


        }
        // for (int i=0;i<ji.size();i++)
        // {
        //System.out.println(ji.get(i).getOrderSn()+" "+ji.get(i).getProductPriceTotal()+ji.get(i).getShopId());
        // }

      /*  List<OdsOrderGoods>fuck=new ArrayList<OdsOrderGoods>();
        for (int i = 0; i < num6.size(); i++) {
            OdsOrderGoods goods = new OdsOrderGoods();
            long id = 0;
            for (int y = 0; y < ji.size(); y++) {
                if (ji.get(y).getShopId().toString().equals(shop.get(i))) {
                    id = ji.get(y).getOrderId();
                    break;
                }
            }
            goods.setOrderId(id);
            goods.setProductId((long) things.get(i));
            goods.setGoodsPrice(price6.get(i));
            goods.setGoodsNum(num6.get(i));
            //商品重量，商品属性可以根据商品id查询商品规格
            goods.setGoodsStatus((byte)0);
            //保存到数据库
            fuck.add(goods);
        }

        for (int i=0;i<fuck.size();i++){
            System.out.println(fuck.get(i).getOrderId());
        }*/


    }

}
